package com.cowain.base.controller.system;

import com.cowain.base.core.controller.BaseController;
import com.cowain.base.core.domain.AjaxResult;
import com.cowain.base.core.domain.dto.SysMenuDto;
import com.cowain.base.core.domain.entity.SysMenuEntity;
import com.cowain.base.core.domain.vo.SysMenuVo;
import com.cowain.system.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author songxz
 * @date 2022/11/9
 */
@Api(tags = {"菜单按钮"})
@RestController
@RequestMapping("/system/menu")
public class SysMenuController extends BaseController {

    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation("查询")
    @GetMapping("/list")
    public AjaxResult list(SysMenuVo menu) {
        List<SysMenuDto> menus = sysMenuService.selectMenuList(menu);
        return AjaxResult.success(menus);
    }


    @ApiOperation("查询单个信息")
    @GetMapping(value = "/{menuId}")
    public AjaxResult getInfo(@PathVariable Long menuId) {
        return AjaxResult.success(sysMenuService.selectMenuById(menuId));
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(SysMenuVo menu) {
        List<SysMenuDto> menus = sysMenuService.selectMenuList(menu);
        return AjaxResult.success(sysMenuService.buildMenuTreeSelect(menus));
    }

    @ApiOperation("新增")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysMenuEntity menu) {
        return toAjax(sysMenuService.insertMenu(menu));
    }

    @ApiOperation("修改")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysMenuEntity menu) {
        return toAjax(sysMenuService.updateMenu(menu));
    }

    @ApiOperation("删除")
    @DeleteMapping("/{menuId}")
    public AjaxResult remove(@PathVariable("menuId") Long menuId) {
        return toAjax(sysMenuService.deleteMenuById(menuId));
    }
}
